﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FinalPointSet.Model.Constant;
using FinalPointSet.Model.Constant.Message;
using FinalPointSet.Model.Constants;
using FinalPointSet.Model.Manager;
using FinalPointSet.Model.Model.MockData;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using static FinalPointSet.Model.Manager.MessageManager;
namespace FinalPointSet.ViewModel.Page.MockData
{
    public partial class MockFieldClassifySettingPageViewModel : ObservableObject
    {
        /// <summary>
        /// 划分规则集合
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<MockFieldClassify> _MockFieldClassifies;
        /// <summary>
        /// 当前选中的划分规则
        /// </summary>
        [ObservableProperty]
        private MockFieldClassify? _MockFieldClassify;
        /// <summary>
        /// 当前选中的划分规则
        /// </summary>
        [ObservableProperty]
        private List<ClassfiyType> _ClassfiyTypes = [];
        public MockFieldClassifySettingPageViewModel()
        {
            MockFieldClassifies = new(AssetManager.Instance.GetJsonObjectList<MockFieldClassify>());
            ClassfiyTypes = new List<ClassfiyType>(System.Enum.GetValues(typeof(ClassfiyType)).Cast<ClassfiyType>());
        }

        #region 命令
        /// <summary>
        /// 添加数据
        /// </summary>
        [RelayCommand]
        private void Add()
        {
            if (MockFieldClassifies == null)
                return;

            int maxID = MockFieldClassifies.Count > 0 ? MockFieldClassifies.Max(e => e.ID + 1) : 1001;
            MockFieldClassify mfc = new()
            {
                ID = maxID,
                Name = $"类型{MockFieldClassifies.Count + 1}",
                ClassfiyType = ClassfiyType.Other
            };
            MockFieldClassifies.Add(mfc);
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"新增类型划分[{mfc.Name}]成功!"));
            Debug.Print($"MockFieldClassifySettingPageViewModel.Add(), 新增类型划分[{mfc.Name}]成功!");
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="mfc"></param>
        [RelayCommand]
        private void Delete(MockFieldClassify mfc)
        {
            Debug.Print($"MockFieldClassifySettingPageViewModel.Delete(), 移除类型划分:{mfc.Name}");
            MockFieldClassifies.Remove(mfc);
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"移除类型划分:[{mfc.Name}]成功!"));
        }

        /// <summary>
        /// 保存
        /// </summary>
        [RelayCommand]
        private void Save()
        {
            if (MockFieldClassifies == null)
            {
                Debug.Print("MockFieldClassifySettingPageViewModel.Save(),类型划分集合为空");
                return;
            }

            AssetManager.Instance.UpdateJsonObjectList(MockFieldClassifies.ToList());
            AssetManager.Instance.SaveJsonObjectList<MockFieldClassify>();
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"保存{MockFieldClassifies.Count}条数据成功!"));
        }
    }
    #endregion
}
